/**
 * Copyright 2009 University of Washington This code is released under the GPL version 3 - with a copy of the license
 * included
 * 
 * @author Maya Hao Li
 */
package seedpod.webapp.view.seedpodwidgets;

import seedpod.exceptions.InvalidWidgetException;
import seedpod.model.AVPair;
import seedpod.model.ProtegeValueType;
import seedpod.model.SeedpodModel.RdbCls.FormWidget;
import seedpod.model.SeedpodModel.RdbCls.ViewWidget;

public class BooleanWidget extends Radio {

	public BooleanWidget() {
	// TODO Auto-generated constructor stub
	}

	public BooleanWidget(AVPair avpair) {
		super( avpair );
	}

	@Override
	public FormWidget getFormWidgetType() {
		return FormWidget.BOOLEAN;
	}

	@Override
	public ViewWidget getViewWidgetType() {
		return null;
	}

	@Override
	public String render() {
		int numSymbol = !getAttribute().isNullable() ? 2 : 3;
		boolean hasValueChecked = false;
		String[] symbols = { "yes", "no", "n/a" };

		String view = "";
		String value = getData().toString();

		for ( int c = 0; c < numSymbol; c++ ) {
			view += " <input " + ATT( "id", _widgetID ) + ATT( "name", _widgetID ) + ATT( "type", "radio" ) + ATT(
					"value", symbols[c] );

			if ( symbols[c].equalsIgnoreCase( value ) ) {
				view += "checked ";
				hasValueChecked = true;
			}

			// check n/a if nothing else has been checked.
			if ( hasValueChecked == false && numSymbol == 2 ) view += "checked";

			view += " > ";
			view += symbols[c];
			view += "<br>";
		}
		return view;
	}

	@Override
	public void validateWidget() throws InvalidWidgetException {
		if ( !getAttribute().getProtegeValueType().equals( ProtegeValueType.Boolean ) )
			throw new InvalidWidgetException( "(E) Type must be boolean for this widget." );
	}

}
